package com.rachel.viewpagerutils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/10/18.
 */

public class CusViewPager extends RelativeLayout implements ViewPager.OnPageChangeListener {
    private static final String TAG = "zsr";
    /**
     * UI
     */
    private ViewPager mViewPager;
    private LinearLayout mLinearLayout;
    private Button mStartBtn;

    /**
     * Data
     */
    private List<View> mViews;
    private Context mContext;
    private List<ImageView> mImageViews = new ArrayList<>();

    public CusViewPager(Context context) {
        this(context,null);
    }

    public CusViewPager(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CusViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.cus_pager,this,false);
        addView(view);

        initView(view);
    }
    private interface onBtnListener{
        void onclick();
    }
    public View getBtnView(){
        return mStartBtn;
    }

    private void initView(View view) {

        mViewPager = (ViewPager) view.findViewById(R.id.cus_content_viewpager);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.cus_bottom_ly);
        mStartBtn = (Button) view.findViewById(R.id.start_btn);
    }

    public void setData(CusPagerData data){
        mViews = data.views;
        if (data.isGlide){
            GlidePagerAdapter adapter = new GlidePagerAdapter(mViews);
            mViewPager.setAdapter(adapter);
            mViewPager.addOnPageChangeListener(this);
            if (data.iscircle){
                showBottomCircle(mViews);
                mLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
            }
        }

    }

    private void showBottomCircle(List<View> views) {

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(15,0,0,0);
        for (int i = 0; i < views.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(R.drawable.cus_bottom_selector);
            imageView.setLayoutParams(params);
            if (i == 0){
                imageView.setSelected(true);
            }else{
                imageView.setSelected(false);
            }

            mLinearLayout.addView(imageView);
            mImageViews.add(imageView);

        }

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.d(TAG, "onPageSelected: "+position+" "+mImageViews.size());

        for (int i = 0; i < mImageViews.size(); i++) {
            ImageView imageView = mImageViews.get(i);
            if (i == position){
                imageView.setSelected(true);
            }else{
                imageView.setSelected(false);
            }
        }
        if (position == mImageViews.size() - 1){
            mStartBtn.setVisibility(VISIBLE);
            mLinearLayout.setVisibility(GONE);
            ObjectAnimator animator = ObjectAnimator.ofFloat(mStartBtn,"alpha",0,1);
            animator.setDuration(500);
            animator.setInterpolator(new LinearInterpolator());
            animator.start();

        }else{
            mStartBtn.setVisibility(GONE);
            mLinearLayout.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
